<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />

    <title>${title}</title>

    <link rel="stylesheet" href="/wechat/ucenter/coupon.css" />
    <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.js"></script>
</head>
<body>
<div class="coupon-no" onclick="useCoupon(0,0,0)">不使用优惠券</div>
<div class="page_top">
    <img class="icon_problem" src="http://static.52letsgo.cn/zz/user_coupon_image/ic_problem.png"/>
    <a href="http://www.52letsgo.cn/fe/user/fqa/q_coupon.html">使用优惠券说明</a>
</div>
<div class="coupon-container">
    <img class="no_data_img" src="http://static.52letsgo.cn/zz/user_coupon_image/bg_no_data.png" />
    <ul class="coupon-list">


    </ul>
</div>
</body>
<script>
    $(function(){

        var url ='/m/Coupon/getUseCoupon?userId='+${userinfo.id};
        $.get(url, function(result) {
            if(result.msg=="无记录"){
                $(".no_data_img").show();
            }
            else{
                var data = result.data;
                $(".coupon-list").html("");
                for (var i in data){



                    if(data[i].isTimeout==1){
                        continue;
                    }

                    var rules = data[i].rule.split("|");
                    var item = "";
                    for(var j in rules){
                        item += '<li class="rule-item">'+rules[j]+'</li>';
                    }
                    var html = '<li class="coupon-item" data-cond="'+data[i].conditions+ '" data-couponuserid="'+data[i].id+'" data-couponid="'+data[i].couponId+'" data-couponname="'+data[i].name+'" >'+
                            '<div class="coupon-header">'+
                            '<div class="coupon-header-left">'+
                            '<div class="coupon-header-left-top"><span class="RMB">￥<span></span></span>'+data[i].value+'</div>'+
                            '<div class="coupon-header-left-bottom">满'+data[i].conditions+'使用</div>'+
                            '</div>'+
                            '<div class="coupon-header-right">'+
                            '<div class="coupon-header-right-top">'+data[i].name+
                            '</div>'+
                            '<div class="coupon-header-right-bottom">'+
                            ''+data[i].bettwenTime+''+
                            '</div>'+
                            '</div>'+
                            '</div>'+
                            '<div class="coupon-bottom">'+
                            '<div class="coupon-bottom-header">'+
                            '<div class="coupon-bottom-left">使用规则</div>'+
                            '<div class="coupon-bottom-right">'+
                            '<img src="http://static.52letsgo.cn/zz/user_coupon_image/ic_arrow_right_gray.png">'+
                            '</div>'+
                            '</div>'+
                            '<hr>'+
                            '</div>'+
                            '</li>'+
                            '<div class="coupon-header-rule">'+
                            '<ul class="rule-list">'+item
                    '</ul>'+
                    '</div>'
                    var $html = $(html);
                    $(".coupon-list").append($html);
                }
                $(".coupon-bottom-header").on("click", function(e) {
                    e.stopPropagation();
                    $(this).parents(".coupon-item").next(".coupon-header-rule").slideToggle();
                    $(this).find("img").toggleClass("rotate90");
                });
                $(".coupon-item").on("click",function () {

                    var couponUserId = $(this).attr("data-couponuserid");
                    var couponId = $(this).attr("data-couponid");
                    var couponName = $(this).attr("data-couponname");
                    var couponCond = $(this).attr("data-cond");
                    var totalPrice = ${totalPrice};

                    if(totalPrice<couponCond){
                        alert("不符合使用条件");
                        return ;
                    }

                    //使用价格


                    useCoupon(couponUserId,couponId,couponName);
                })
            }
        })
    })



    function useCoupon(couponUserId,couponId,couponName) {
        $.post("/ucenter/chooseCoupon.do",
                {CouponUserId:couponUserId,CouponId:couponId,CouponName:couponName},
                function (data) {
                    if (!data.success){
                        tip("",data.msg,true);
                    }else {
                        window.location.replace("/home/shopCar.do");
                    }
                },"json");
    }
</script>
</html>